package com.gm.interceptor;

import com.gm.model.po.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created by taojie on 17-6-6.
 */
public class GmInterceptor implements HandlerInterceptor {

    Logger logger = LoggerFactory.getLogger("GmInterceptor");

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest,
                             HttpServletResponse httpServletResponse, Object o) throws Exception {

        logger.info("preHandle[{}]", httpServletRequest.getRequestURI());

        HttpSession session = httpServletRequest.getSession();
        User user = (User)session.getAttribute("user");
        logger.info("user[{}]", user == null?null:user.toString());
        if(user == null){
            httpServletResponse.sendRedirect("../jump.html");
            return false;
        }else{
            return true;
        }

    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest,
                           HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest,
                                HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
    }
}
